package qcurepay

import (
    "fmt"
    "errors"
	"encoding/json"
)

type QcurepayNotifyMsg struct {
	RespCode       string  `json:"respCode"`
	RespMsg        string  `json:"respMsg"`
	Version        string  `json:"version"`
	OrgCode        string  `json:"orgCode"`
	MerchCode      string  `json:"merchCode"`
	PayType        string  `json:"payType"`
	ChannelOrderNo string  `json:"channelOrderNo"`
	OrderNo        string  `json:"orderNo"`
	MerchOrderNo   string  `json:"merchOrderNo"`
	Amount         string  `json:"amount"`
	CreateTime     string  `json:"createTime"`
	PayTime        string  `json:"payTime"`
	Sign           string  `json:"sign"`
	Remark         string  `json:"remark"`
}

/*
{"amount":"2","createTime":"2019-03-28 18:45:43","merchCode":"QP0088880000005","merchOrderNo":"331211832553377792","orderNo":"HKDF937182982033883136","orgCode":"66600196","payTime":"2019-03-28 18:45:48","payType":"wxgjpay_public","remark":"订单[331211832553377792]","respCode":"00","respMsg":"成功","sign":"D017003BA64422495190494939BAA076","version":"V1.0"}*/


func ParseQcureNotify(body []byte) (map[string]interface{}, error) {
	rspMap := make(map[string]interface{})
	err := json.Unmarshal(body, &rspMap)
	if err != nil {
		return rspMap, err
	}
	return rspMap, nil
}

func VerifyQcureSign(rspMap map[string]interface{}, appKey string) (map[string]string, error){
	sign := MakeSign(rspMap, appKey)
	if rspMap["sign"] != sign {
	    returnMsg := "sign is invalid"
		return map[string]string{}, errors.New(returnMsg)
	}
	//fmt.Println("get sign:" , rspMap["sign"])
	//fmt.Println("calc sign:", sign)
	reqMap := make(map[string]string)
	for k, v := range rspMap {
		reqMap[k] = fmt.Sprintf("%v", v)
	}
	return reqMap, nil
}
